#!/usr/bin/env python
#
# Checks out existing transactions
#
# Hint: master the ability to choose when to expand or not!
#

import os
import sys
import Tkinter as tk

import admin
import config

class GUI:
    def checkorder(self):
        purchase = admin.view_purchase(self.codefield.get())
        finalstring = ""
        if purchase is not None:
            finalstring += "Cliente: %s (%s)\n" % (purchase[0], purchase[1])
            if purchase[2] != 0:
                finalstring += "Sabonete de Mel: %s unidades\n" % purchase[2]
            if purchase[3] != 0:
                finalstring += "Sabonete de Patchouli: %s unidades\n" % purchase[3]
            if purchase[4] != 0:
                finalstring += "Sabonete de Papoula: %s unidades\n" % purchase[4]
            if purchase[5] != 0:
                finalstring += "Sabonete de Capim-limao: %s unidades\n" % purchase[5]
            if purchase[6] != 0:
                finalstring += "Alcool-gel: %s unidades\n" % purchase[6]

            finalstring += "Status do pedido: %s\n" % purchase[7]
            finalstring += "Criado em: %s" % purchase[8]

            self.resultsfield.config(text=finalstring)

        else:
            self.resultsfield.config(text="Transaction not found")

    def __init__(self):
        self.window = tk.Tk()
        self.window.geometry("400x300")
        self.window.title("Compras existentes")

        self.coderow = tk.Frame(self.window)
        
        self.codelabel = tk.Label(self.coderow, text="Codigo do pedido")
        self.codefield = tk.Entry(self.coderow)

        self.checkbutton = tk.Button(self.coderow, text="Checar",
            command=self.checkorder)

        self.resultsarea = tk.Frame(self.window)
        self.resultslabel = tk.Label(self.resultsarea, text="Detalhes do pedido")
        self.resultsfield = tk.Label(self.resultsarea, text="")

        self.coderow.pack(expand=False)
        self.resultsarea.pack(expand=True, fill=tk.BOTH)
        self.resultslabel.pack()
        self.resultsfield.pack(expand=True, fill=tk.BOTH)

        self.codelabel.pack(side="left")
        self.codefield.pack(side="left")
        self.checkbutton.pack(side="left")

    def main(self):
        self.window.mainloop()

if __name__ == "__main__":
    app = GUI()
    app.main()
